/**
 * Definition for singly-linked list.
 * */
#include <iostream>
using namespace std;
struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}

    void print()
    {
        auto value = this->val;
        auto next = this->next;
        auto start = 10;
        while (next != nullptr)
        {
            value = next->val * start + value;
            start = start * 10;
            next = next->next;
        }
        std::cout << value << std::endl;
    }
};

class Solution
{
public:
    ListNode *addTwoNumbers2(ListNode *l1, ListNode *l2)
    {
        auto one = l1;
        auto two = l2;
        auto jinweiZhi = 0;
        auto Result = new ListNode();
        auto tmp = Result;
        while (1)
        {
            if (one == nullptr && two == nullptr && jinweiZhi == 0) {
                break;
            }
            auto oneV = 0;
            auto twoV = 0;
            if (one != nullptr) {
                oneV = one->val;
                one = one->next;
            }
            if (two!= nullptr) {
                twoV = two->val;
                two = two->next;
            }
            auto v = oneV + twoV + jinweiZhi;
            jinweiZhi = v / 10;
            v = v % 10;
            tmp->val = v;
            if (one != nullptr || two != nullptr || jinweiZhi != 0) {
                tmp->next = new ListNode();
                tmp = tmp->next;
            }
        }
        return Result;
    }
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
    {
        unsigned long long one = l1->val;
        unsigned long long two = l2->val;
        unsigned long long start = 10;
        auto next = l1->next;
        while (next != nullptr)
        {
            one = one + next->val * start;
            next = next->next;
            start = start * 10;
        }
        start = 10;
        next = l2->next;
        ;
        while (next != nullptr)
        {
            two = two + next->val * start;
            next = next->next;
            start = start * 10;
        }
        auto three = one + two;
        start = 1;
        auto lResult = new ListNode();
        auto tmp = lResult;
        while (1)
        {
            auto v = (three / start) % 10;
            std::cout << v << std::endl;
            tmp->val = v;
            start = start * 10;
            if (start > three)
            {
                break;
            }
            tmp->next = new ListNode();
            tmp = tmp->next;
        }
        return lResult;
    }
};

int main()
{
    auto one = new ListNode(2);
    auto three = new ListNode(3);
    auto two = new ListNode(4);
    one->next = two;
    two->next = three;
    auto l2 = new ListNode(5);
    auto l3 = new ListNode(6);
    auto l4 = new ListNode(4);
    l2->next = l3;
    l3->next = l4;
    auto s = Solution();
    one->print();
    l2->print();
    auto result = s.addTwoNumbers2(one, l2);
    result->print();
    return 0;
}